package com.app.synclistview;

import java.util.Vector;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

public class MainActivity extends AbstractCommonActivity {

	private ListView bookListView;
	private Vector<Book> books = null;
	private BookItemAdapter adapter;
	private LoadStateView loadStateView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		
		books = getData();
		adapter = new BookItemAdapter(this, books, bookListView);
		bookListView = (ListView)findViewById(R.id.viewBookList);
		bookListView.setAdapter(adapter);
		loadStateView = (LoadStateView)findViewById(R.id.downloadStatusBox);
		
		loadStateView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				reload();
			}
		});
	}

	private void reload(){
		adapter.clean();
		loadStateView.startLoad();
		new Thread(new Runnable(){
			@Override
			public void run() {
				try {
					Thread.sleep(2*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				adapter.addBook(getData());
				sendMessage(REFRESH_LIST);
			}
		}).start();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public Vector<Book> getData(){
		Vector<Book> tempBooks = new Vector<Book>();
		
		for(int i=0;i<10;i++){
			Book book1 = new Book();
			book1.setBook_name("吞噬星空"+i);
			book1.setOut_book_url("http://www.pfwx.com/bookinfo/11/11000.html");
			book1.setOut_book_pic("http://www.pfwx.com/files/article/image/11/11000/11000s.jpg");
			tempBooks.add(book1);
			
			Book book2 = new Book();
			book2.setBook_name("仙逆"+i);
			book2.setOut_book_url("http://www.pfwx.com/bookinfo/9/9760.html");
			book2.setOut_book_pic("http://www.pfwx.com/files/article/image/9/9760/9760s.jpg");
			tempBooks.add(book2);
			
			Book book3 = new Book();
			book3.setBook_name("武动乾坤"+i);
			book3.setOut_book_url("http://www.pfwx.com/bookinfo/13/13939.html");
			book3.setOut_book_pic("http://www.pfwx.com/files/article/image/13/13939/13939s.jpg");
			tempBooks.add(book3);
			
			Book book4 = new Book();
			book4.setBook_name("凡人修仙传"+i);
			book4.setOut_book_url("http://www.pfwx.com/bookinfo/3/3237.html");
			book4.setOut_book_pic("http://www.pfwx.com/files/article/image/3/3237/3237s.jpg");
			tempBooks.add(book4);
			
			Book book5 = new Book();
			book5.setBook_name("遮天"+i);
			book5.setOut_book_url("http://www.pfwx.com/bookinfo/11/11381.html");
			book5.setOut_book_pic("http://www.pfwx.com/files/article/image/11/11381/11381s.jpg");
			tempBooks.add(book5);
		}		
		return tempBooks;
	}

	private static final int REFRESH_LIST = 0x10001;
	private static final int SHOW_LOAD_STATE_VIEW = 0x10003;
	private static final int HIDE_LOAD_STATE_VIEW = 0x10004;
	
	@Override
	public void handleOtherMessage(int flag) {
		switch (flag) {
		case REFRESH_LIST:
			adapter.notifyDataSetChanged();
			loadStateView.stopLoad();
			if(adapter.getCount() == 0){
				loadStateView.showEmpty();
			}
			break;
		case SHOW_LOAD_STATE_VIEW:
			loadStateView.startLoad();
			break;
		case HIDE_LOAD_STATE_VIEW:
			loadStateView.stopLoad();
			break;

		default:
			break;
		}
	}
}
